<template>
  <a-button :loading="uploading" @click="exportExcel" type="primary" ghost>导出数据</a-button>
</template>
<script>
import XLSX from 'xlsx'
import moment from 'moment'
import { getLoanDetails } from '@/api/common'
export default {
  name: 'loanDetailExport',
  props: ['columns'],
  data: () => ({
    uploading: false
  }),
  methods: {
    async exportExcel() {
      this.uploading = true
      /* 从表生成工作簿对象 */
      const form = {
        projectId: this.$route.query.id
      }
      const res = await getLoanDetails(form)
      const _list = res.result?.data?.list || []
      const exportData = _list.map((el, i) => {
        el.calaDate = el.calaDate ? moment(el.calaDate * 1000).format('YYYY-MM-DD') : '-'
        el.index = i
        const res = {}
        this.columns.forEach(col => {
          if (el[col.dataIndex]) {
            res[col.title] = el[col.dataIndex]
          }
        })
        return res
      })
      // console.log(exportData)

      var ws = XLSX.utils.json_to_sheet(exportData)
      /* 获取二进制字符串作为输出 */
      var wb = XLSX.utils.book_new() /*新建book*/
      XLSX.utils.book_append_sheet(wb, ws, '贷款明细列表') /* 生成xlsx文件(book,sheet数据,sheet命名) */
      XLSX.writeFile(wb, '贷款明细列表.xlsx') /*写文件(book,xlsx文件名称)*/
      this.uploading = false
    }
  }
}
</script>
<style lang="scss" scoped></style>
